#include "main.h"
#include "dap_main.h"
#include "usart.h"

// 定义UART2接收缓冲区，大小为2KB，32字节对齐
static __ALIGNED(32) uint8_t uart3_recv_buff[2 * 1024];
// 定义全局变量，用于记录UART发送数据的长度
static volatile uint32_t g_uart_tx_transfer_length = 0;

extern UART_HandleTypeDef huart3;
extern DMA_HandleTypeDef hdma_usart3_rx;
extern DMA_HandleTypeDef hdma_usart3_tx;

// 定义UART2的DMA接收相关的宏
#define UART3_RX_DMA_RCU            RCU_DMA1
#define UART3_RX_DMA                DMA1
#define UART3_RX_DMA_CH             DMA_CHANNEL_4
#define UART3_RX_DMA_CH_IRQ         DMA1_Stream1_IRQn
#define UART3_RX_DMA_CH_IRQ_HANDLER DMA1_Stream1_IRQHandler

// CDC UART配置回调函数
void chry_dap_usb2uart_uart_config_callback(struct cdc_line_coding *line_coding)
{
    // USART配置
//    HAL_UART_DeInit(&huart3);
	
	huart3.Instance = USART3;
	huart3.Init.BaudRate = line_coding->dwDTERate;
    if(line_coding->bParityType == 1)
    {
		huart3.Init.Parity = UART_PARITY_ODD;
    }else if (line_coding->bParityType == 2)
    {
		huart3.Init.Parity = UART_PARITY_EVEN;
    }else
    {
		huart3.Init.Parity = UART_PARITY_NONE;
    }
	huart3.Init.WordLength = line_coding->bDataBits;
   
    if(line_coding->bCharFormat == 1)
    {
		//not suppore
		huart3.Init.StopBits = UART_STOPBITS_1;
    }else if (line_coding->bCharFormat == 2)
    {
		huart3.Init.StopBits = UART_STOPBITS_2;
    }else
    {
		huart3.Init.StopBits = UART_STOPBITS_1;
    }
	
	huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
	huart3.Init.OverSampling = UART_OVERSAMPLING_16;
	huart3.Init.Mode = UART_MODE_TX_RX;
	if (HAL_UART_Init(&huart3) != HAL_OK)
    {
    Error_Handler();
    }

    // 配置UART接收DMA
	HAL_UART_Receive_DMA(&huart3, uart3_recv_buff, sizeof(uart3_recv_buff));
}

// USART3中断处理函数
void USART3_IRQHandler(void)
{
    static volatile uint32_t receive_len = 0;
    // 检查USART空闲中断标志
    if (__HAL_UART_GET_FLAG(&huart3,UART_FLAG_IDLE) != RESET)
    {
		  __HAL_UART_CLEAR_IDLEFLAG(&huart3);
		  HAL_UART_AbortReceive(&huart3);
		  receive_len = sizeof(uart3_recv_buff) - __HAL_DMA_GET_COUNTER(&hdma_usart3_rx);    // 计算接收的数据长度
		  chry_ringbuffer_write(&g_uartrx, uart3_recv_buff,sizeof(uart3_recv_buff) - __HAL_DMA_GET_COUNTER(&hdma_usart3_rx));
		  HAL_UART_Receive_DMA(&huart3,uart3_recv_buff,sizeof(uart3_recv_buff));         // 开启DMA继续接收

    }
	HAL_UART_IRQHandler(&huart3);
	__HAL_UART_CLEAR_OREFLAG(&huart3);
}


// CDC UART通过DMA发送数据函数
void chry_dap_usb2uart_uart_send_bydma(uint8_t *data, uint16_t len)
{

	HAL_UART_Transmit_DMA(&huart3,data,len);
	
	
	// 记录要发送的数据长度
    g_uart_tx_transfer_length = len;

}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart->Instance == USART3)
	{
		chry_dap_usb2uart_uart_send_complete(g_uart_tx_transfer_length);
	}
}

// UART3发送DMA中断处理函数
void DMA1_Stream3_IRQHandler(void)
{
	HAL_DMA_IRQHandler(&hdma_usart3_tx);
}

// UART3接收DMA中断处理函数
void DMA1_Stream1_IRQHandler(void)
{
	HAL_DMA_IRQHandler(&hdma_usart3_rx);
}

//// UART3发送DMA中断处理函数
//void DMA1_Stream3_IRQHandler(void)
//{
//    // 检查DMA传输完成标志
//    if(__HAL_DMA_GET_FLAG(&hdma_usart3_tx,DMA_FLAG_TCIF3_7)!= RESET)
//    {
//        // 清除DMA传输完成标志
//        __HAL_DMA_CLEAR_FLAG(&hdma_usart3_tx,DMA_FLAG_TCIF3_7);
//        // 调用发送完成回调函数
//        chry_dap_usb2uart_uart_send_complete(g_uart_tx_transfer_length);
//    }
//}
